/***************************************
 2010-4-23
 ****************************************/
package com.happy.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.orm.hibernate.SpringSecurityUtils;
import com.happy.common.security.UserConnectManage;

public class IPFilter extends HttpServlet implements Filter {
	/**
	 * 
	 */

	/** 默认无参构造函数 **/
	public IPFilter() {
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		response.setCharacterEncoding("utf-8");
		String address = request.getRemoteAddr();
		HttpServletRequest re = (HttpServletRequest) request;
		String dir = re.getRequestURI();
		String users = SpringSecurityUtils.getCurrentUserName();
			if (!UserConnectManage.checkLoginValidate(request.getRemoteAddr(),
					users)) {
				RequestDispatcher requestDispatcher = request
						.getRequestDispatcher("/common/errors.jsp");
				requestDispatcher.forward(request, response);

			} else {
				//request.setCharacterEncoding("utf-8");
				chain.doFilter(request, response);
				//response.setCharacterEncoding("utf-8");
			}
		
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}
}
